package com.csyf.manager.channel.cspay;

import com.csyf.manager.channel.base.PaymentVo;
import com.csyf.manager.channel.base.TrandeVo;
import com.csyf.manager.common.CommonConst;
import com.csyf.manager.entity.SysAgentChannel;
import com.csyf.manager.entity.SysPaymentChannel;
import com.csyf.manager.exception.ApiValidateException;
import com.csyf.manager.service.impl.RedisService;
import com.csyf.manager.utils.HttpHelper;
import com.csyf.manager.utils.SignUtils;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.TreeMap;

/**
 * @Author: casper
 * @Description: 主通道
 * @Date: Created in 2019/3/7 17:33
 */
@Slf4j
@Service
public class CspayBiz {

    /**
     * 交易地址
     **/
    public static String TRADE_URL = "trade.php";

    /**
     * 代付地址
     **/
    public static String PAYMENT_URL = "df.php";

    /**
     * 代付查询地址
     **/
    public static String QUERY_PAYMENT_URL = "dfQuery.php";


    @Autowired
    private RedisService redisService;

    /**
     * 交易
     *
     * @param vo
     * @param srcMerchId
     * @return
     * @throws ApiValidateException
     */
    public String dopay(TrandeVo vo, String srcMerchId) throws ApiValidateException {
        String channelStr = redisService.getSrcChannelInfo("", srcMerchId);
        SysAgentChannel sysAgentChannel = new Gson().fromJson(channelStr, SysAgentChannel.class);

        Map<String, String> reqMap = new TreeMap<>();
        reqMap.put("mch_id", sysAgentChannel.getMerchantNo());
        reqMap.put("total_fee", vo.getTotal_fee());
        reqMap.put("service", vo.getService());
        reqMap.put("out_trade_no", vo.getOut_trade_no());
        reqMap.put("sign_type", vo.getSign_type());
        reqMap.put("notify_url", sysAgentChannel.getNotifyUrl());
        reqMap.put("body", vo.getBody());
        reqMap.put("return_url", vo.getReturn_url());
        reqMap.put("bankno", vo.getBankno());
        reqMap.put("cardNo", vo.getCardNo());
        reqMap.put("bank_linked", vo.getBank_linked());

        String sign = SignUtils.map2signkey(reqMap, sysAgentChannel.getSignKey());
        reqMap.put("sign", sign);
        log.debug("{}订单号ECPAY请求上游报文格式：{}", vo.getOut_trade_no(), reqMap);
        String result = HttpHelper.doPostForm(sysAgentChannel.getPublicKey() + TRADE_URL, reqMap, CommonConst.UTF8);
        log.debug("{}订单号ECPAY上游响应报文格式：{}", vo.getOut_trade_no(), result);

        Map<String, String> resultMap = new Gson().fromJson(result, Map.class);
        String payinfo = "";
        String ret_code = resultMap.get("ret_code");
        if ("SUCCESS".equals(ret_code)) {
            payinfo = resultMap.get("payinfo");
        }else{
            throw new ApiValidateException(resultMap.get("ret_message"));
        }
        return payinfo;
    }

    /**
     * 代付
     *
     * @param vo
     * @param srcMchId
     * @return
     */
    public Map<String, String> dodfpay(PaymentVo vo, String srcMchId) {
        String channelString = redisService.getPaymentChannelInfo("", srcMchId);
        SysPaymentChannel sysPaymentChannel = new Gson().fromJson(channelString, SysPaymentChannel.class);

        Map<String, String> reqMap = new TreeMap<>();
        reqMap.put("mch_id", sysPaymentChannel.getMerchantNo());
        reqMap.put("trans_money", vo.getTrans_money());
        reqMap.put("service", vo.getService());
        reqMap.put("out_trade_no", vo.getOut_trade_no());
        reqMap.put("account_name", vo.getAccount_name());
        reqMap.put("bank_card", vo.getBank_card());
        reqMap.put("bankCode", vo.getBankCode());
        reqMap.put("bank_name", vo.getBank_name());
        reqMap.put("bankBranchname", vo.getBankBranchname());
        reqMap.put("bankProvince", vo.getBankProvince());
        reqMap.put("bankCity", vo.getBankCity());
        reqMap.put("bank_linked", vo.getBank_linked());
        reqMap.put("phoneNo", vo.getPhoneNo());
        reqMap.put("idcard", vo.getIdcard());

        String sign = SignUtils.map2signkey(reqMap, sysPaymentChannel.getSignKey());
        reqMap.put("sign", sign);

        log.debug("[{}]ECPAY代付支付上游请求报文：{}", vo.getOut_trade_no(), reqMap.toString());
        String result = HttpHelper.doPostForm(sysPaymentChannel.getPublicKey() + PAYMENT_URL, reqMap, CommonConst.UTF8);
        log.debug("[{}]ECPAY代付支付上游响应报文：{}", vo.getOut_trade_no(), result);

        Map<String, String> resultMap = new Gson().fromJson(result, Map.class);
        return resultMap;
    }

    /**
     * 代付查询
     *
     * @param orderNo
     * @param mchId
     * @param srcMerchId
     * @return
     */
    public Map<String, String> querydfOrder(String orderNo, String mchId, String srcMerchId) {
        String channelString = redisService.getPaymentChannelInfo("", srcMerchId);
        SysPaymentChannel sysPaymentChannel = new Gson().fromJson(channelString, SysPaymentChannel.class);

        Map<String, String> reqMap = new TreeMap<>();
        reqMap.put("mch_id", sysPaymentChannel.getMerchantNo());
        reqMap.put("out_trade_no", orderNo);

        String sign = SignUtils.map2signkey(reqMap, sysPaymentChannel.getSignKey());
        reqMap.put("sign", sign);
        log.debug("[{}]ECPAY代付查询上游请求报文：{}", orderNo, reqMap.toString());
        String result = HttpHelper.doPostForm(sysPaymentChannel.getPublicKey() + QUERY_PAYMENT_URL, reqMap, CommonConst.UTF8);
        log.debug("[{}]ECPAY代付查询上游响应报文：{}", orderNo, result);
        Map<String, String> resultMap = new Gson().fromJson(result, Map.class);
        return resultMap;
    }


}
